package com.ruoyi.web.controller.meetings;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.MeetingsManagement;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IMeetingsManagementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 会议管理Controller
 * 
 * @author ruoyi
 * @date 2020-07-21
 */
@Controller
@RequestMapping("/ruoyi/management")
public class MeetingsManagementController extends BaseController
{
    private String prefix = "ruoyi/management";

    @Autowired
    private IMeetingsManagementService meetingsManagementService;

    @RequiresPermissions("ruoyi:management:view")
    @GetMapping()
    public String management()
    {
        return prefix + "/management";
    }

    /**
     * 查询会议管理列表
     */
    @RequiresPermissions("ruoyi:management:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeetingsManagement meetingsManagement)
    {
        startPage();
        List<MeetingsManagement> list = meetingsManagementService.selectMeetingsManagementList(meetingsManagement);
        return getDataTable(list);
    }

    /**
     * 导出会议管理列表
     */
    @RequiresPermissions("ruoyi:management:export")
    @Log(title = "会议管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MeetingsManagement meetingsManagement)
    {
        List<MeetingsManagement> list = meetingsManagementService.selectMeetingsManagementList(meetingsManagement);
        ExcelUtil<MeetingsManagement> util = new ExcelUtil<MeetingsManagement>(MeetingsManagement.class);
        return util.exportExcel(list, "会议管理");
    }

    /**
     * 新增会议管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存会议管理
     */
    @RequiresPermissions("ruoyi:management:add")
    @Log(title = "会议管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, MeetingsManagement meetingsManagement)throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);

        meetingsManagement.setMmUpload(fileName);
        meetingsManagement.setMmUploader(ShiroUtils.getLoginName());
        return toAjax(meetingsManagementService.insertMeetingsManagement(meetingsManagement));
    }

    /**
     * 修改会议管理
     */
    @GetMapping("/edit/{mmId}")
    public String edit(@PathVariable("mmId") Long mmId, ModelMap mmap)
    {
        MeetingsManagement meetingsManagement = meetingsManagementService.selectMeetingsManagementById(mmId);
        mmap.put("meetingsManagement", meetingsManagement);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议管理
     */
    @RequiresPermissions("ruoyi:management:edit")
    @Log(title = "会议管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MeetingsManagement meetingsManagement)
    {
        return toAjax(meetingsManagementService.updateMeetingsManagement(meetingsManagement));
    }

    /**
     * 删除会议管理
     */
    @RequiresPermissions("ruoyi:management:remove")
    @Log(title = "会议管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(meetingsManagementService.deleteMeetingsManagementByIds(ids));
    }
    /**
     *
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
}
